package com.mtask.task.config;


import com.mtask.task.service.TaskService;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;

import javax.annotation.Resource;

@Configuration
public class QuartzJobStartListener implements ApplicationListener<ContextRefreshedEvent> {
    @Resource
    private TaskService taskService;


    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        taskService.startJobWithDb();
    }

    @Bean
    public Scheduler scheduler() throws SchedulerException {
        Scheduler scheduler = new StdSchedulerFactory().getScheduler();
        scheduler.start();
        return scheduler;
    }

}
